home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / mac / Sample Code / QuickTime / QuickTimeIntro / Play Movie w⁄Controller / Completed Lab / OpenMovieInWindow.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  6.0 KB  |  192 lines  |  [TEXT/CWIE]

  1. // Play Movie with Controller Sample
  2. // Based on QTShell
  3. // WWDC 2000
  4.  
  5. #include "ComApplication.h"
  6. #include "ComFramework.h"
  7. #include "MacFramework.h"
  8.  
  9. //////////
  10. //
  11. // OpenMovieInWindow
  12. // Open a movie in a new movie window; return true if successful.
  13. //
  14. // This function is called from several places in our framework. The following combinations are possible:
  15. //    * theMovie == NULL, theFSSpec == NULL: no movie, no file; elicit a movie file from user and open it
  16. //    * theMovie != NULL, theFSSpec == NULL: new movie, no file (yet)
  17. //    * theMovie == NULL, theFSSpec != NULL: no movie, but we have an FSSpec; so just open the specified movie file
  18. //    * theMovie != NULL, theFSSpec != NULL: new movie, theFSSpec contains (at least) the movie name
  19. //
  20. //////////
  21.  
  22. Boolean OpenMovieInWindow (Movie theMovie, FSSpec *theFSSpec)
  23. {
  24.     WindowObject            myWindowObject = NULL;
  25.     Movie                    myMovie = NULL;
  26.     MovieController            myMC = NULL;
  27.     GraphicsImportComponent    myImporter = NULL;
  28.     WindowReference            myWindow = NULL;
  29.     FSSpec                    myFSSpec;
  30.     short                    myRefNum = kInvalidFileRefNum;
  31.     short                    myResID = 0;
  32.     OSType                     myTypeList[] = {kQTFileTypeMovie, kQTFileTypeQuickTimeImage};
  33.     short                    myNumTypes = 2;
  34.     GrafPtr                    mySavedPort;
  35.     Rect                    myRect = {0, 0, 0, 0};
  36.     Point                    myPoint;
  37.     QTFrameFileFilterUPP    myFileFilterUPP = NULL;
  38.     OSErr                    myErr = noErr;
  39.  
  40. #if TARGET_OS_MAC
  41.     myNumTypes = 0;
  42. #endif
  43.  
  44.     // get the current port; we may need to restore it if we cannot successfully create a new window
  45.     GetPort(&mySavedPort);
  46.     
  47.     // if we got neither a movie nor an FSSpec passed in, prompt the user for a movie file
  48.     if ((theMovie == NULL) && (theFSSpec == NULL)) {
  49.         myFileFilterUPP = QTFrame_GetFileFilterUPP((ProcPtr)QTFrame_FilterFiles);
  50.     
  51.         myErr = QTFrame_GetOneFileWithPreview(myNumTypes, (QTFrameTypeListPtr)myTypeList, &myFSSpec, myFileFilterUPP);
  52.     
  53.         if (myFileFilterUPP != NULL)
  54.             DisposeNavObjectFilterUPP(myFileFilterUPP);
  55.  
  56.         if (myErr != noErr)
  57.             goto bail;
  58.     }
  59.     
  60.     // if we got an FSSpec passed in, copy it into myFSSpec
  61.     if (theFSSpec != NULL) {
  62.         FSMakeFSSpec(theFSSpec->vRefNum, theFSSpec->parID, theFSSpec->name, &myFSSpec);        
  63.     }
  64.  
  65.     // if we got no movie passed in, read one from the specified file
  66.     if (theMovie == NULL) {
  67.  
  68.         // see if the FSSpec picks out an image file; if so, skip the movie-opening code
  69.         myErr = GetGraphicsImporterForFile(&myFSSpec, &myImporter);
  70.         if (myImporter != NULL)
  71.             goto gotImageFile;
  72.             
  73.         // ideally, we'd like read and write permission, but we'll settle for read-only permission
  74.         myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdWrPerm);
  75.         if (myErr != noErr)
  76.             myErr = OpenMovieFile(&myFSSpec, &myRefNum, fsRdPerm);
  77.  
  78.         // if we couldn't open the file with even just read-only permission, bail....
  79.         if (myErr != noErr)
  80.             goto bail;
  81.  
  82.         // now fetch the first movie from the file
  83.         myResID = 0;
  84.         myErr = NewMovieFromFile(&myMovie, myRefNum, &myResID, NULL, newMovieActive, NULL);
  85.         if (myErr != noErr)
  86.             goto bail;
  87.     } else {
  88.         myMovie = theMovie;
  89.     }
  90.  
  91.     //////////
  92.     //
  93.     // at this point, myMovie is an open movie, but myFSSpec may or may not be a valid FSSpec
  94.     //
  95.     //////////
  96.     
  97.     // set the default progress procedure for the movie
  98.     SetMovieProgressProc(myMovie, (MovieProgressUPP)-1, 0);
  99.         
  100. gotImageFile:
  101.     // create a new window to display the movie in
  102.     myWindow = QTFrame_CreateMovieWindow();
  103.     if (myWindow == NULL)
  104.         goto bail;
  105.     
  106.     myWindowObject = QTFrame_GetWindowObjectFromWindow(myWindow);
  107.     if (myWindowObject == NULL)
  108.         goto bail;
  109.     
  110.     // set the window title
  111.     QTFrame_SetWindowTitleFromFSSpec(myWindow, &myFSSpec, true);
  112.  
  113.     // make sure the movie or image file uses the window GWorld
  114.     if (myMovie != NULL)
  115.         SetMovieGWorld(myMovie, (CGrafPtr)QTFrame_GetPortFromWindowReference(myWindow), NULL);
  116.  
  117.     if (myImporter != NULL)
  118.         GraphicsImportSetGWorld(myImporter, (CGrafPtr)QTFrame_GetPortFromWindowReference(myWindow), NULL);
  119.  
  120.     // create and configure the movie controller
  121.     myMC = CreateController(myMovie, myWindow, true);
  122.         
  123.     // store movie info in the window record
  124.     (**myWindowObject).fMovie = myMovie;
  125.     (**myWindowObject).fController = myMC;
  126.     (**myWindowObject).fGraphicsImporter = myImporter;
  127.     (**myWindowObject).fFileResID = myResID;
  128.     (**myWindowObject).fFileRefNum = myRefNum;
  129.     (**myWindowObject).fCanResizeWindow = true;
  130.     (**myWindowObject).fIsDirty = false;
  131.     (**myWindowObject).fIsQTVRMovie = QTUtils_IsQTVRMovie(myMovie);
  132.     (**myWindowObject).fInstance = NULL;
  133.     (**myWindowObject).fAppData = NULL;
  134.     (**myWindowObject).fFileFSSpec = myFSSpec;
  135.     
  136.     // do any application-specific window object initialization
  137.     QTApp_SetupWindowObject(myWindowObject);
  138.     
  139.     // size the window to fit the movie and controller
  140.     QTFrame_SizeWindowToMovie(myWindowObject);
  141.  
  142.     // set the movie's play hints to allow dynamic resizing
  143.     SetMoviePlayHints(myMovie, hintsAllowDynamicResize, hintsAllowDynamicResize);
  144.  
  145.     // set the movie's position, if it has a 'WLOC' user data atom
  146.     myErr = QTUtils_GetWindowPositionFromFile(myMovie, &myPoint);
  147.  
  148.     // show the window
  149. #if TARGET_OS_MAC
  150.     MoveWindow(myWindow, myPoint.h, myPoint.v, false);
  151.     ShowWindow(myWindow);
  152.     SelectWindow(myWindow);                                // make it front-most, since it's just been created
  153.     InvalWindowRect(myWindow, GetWindowPortBounds(myWindow, &myRect));
  154. #endif
  155. #if TARGET_OS_WIN32
  156.     SetWindowPos(myWindow, 0, myPoint.h, myPoint.v, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  157.     ShowWindow(myWindow, SW_SHOW);
  158.     UpdateWindow(myWindow);
  159. #endif
  160.  
  161.     // if the movie is a streamed movie, then start it playing immediately
  162.     if (QTUtils_IsStreamedMovie(myMovie))
  163.         MCDoAction(myMC, mcActionPrerollAndPlay, (void *)GetMoviePreferredRate(myMovie));
  164.         
  165.     return(true);
  166.     
  167. bail:
  168.     if (myWindow != NULL)
  169. #if TARGET_OS_MAC
  170.         DisposeWindow(myWindow);
  171. #endif
  172. #if TARGET_OS_WIN32
  173.         SendMessage(ghWndMDIClient, WM_MDIDESTROY, (WPARAM)myWindow, 0L);
  174. #endif
  175.         
  176.     if (myMC != NULL)
  177.         DisposeMovieController(myMC);
  178.         
  179.     if (myMovie != NULL)
  180.         DisposeMovie(myMovie);
  181.         
  182.     if (myRefNum != 0)
  183.         CloseMovieFile(myRefNum);
  184.  
  185.     if (myImporter != NULL)
  186.         CloseComponent(myImporter);
  187.         
  188.     MacSetPort(mySavedPort);    // restore the port that was active when this function was called
  189.  
  190.     return(false);
  191. }
  192.